clear
capture restore
set scheme cleanplots, perm

* SET DIRECTORIES
global home "..."

global data "$home/Data/"
global output "$home/Output/"

cd $home

*SET CONTROL VARIABLES
local firm_controls "lev_d2c_1lag_std log_salesg_real_1lag_std size_assets_1lag_std ln_market_cap_q_1lag_std liquidity_1lag_std pcmq_1lag_std recpay2yq_1lag_std d2aq_1lag_std  tobins_q_1lag_std age_std dd_lag_std i.sector i.fqtr"

*lev_d2c_1lag_std

*************
***Options***
*************
*Set ivol measure to use as baseline
local matur m1545
*Set mp shock to use as baseline
local mps ff4

local mps_ints "c.`mps'#c.lev_d2c_1lag_std c.`mps'#c.log_salesg_real_1lag_std c.`mps'#c.size_assets_1lag_std c.`mps'#c.ln_market_cap_q_1lag_std c.`mps'#c.liquidity_1lag_std c.`mps'#c.pcmq_1lag_std c.`mps'#c.recpay2yq_1lag_std c.`mps'#c.d2aq_1lag_std c.`mps'#c.tobins_q_1lag_std c.`mps'#c.age_std c.`mps'#c.dd_lag_std"

*c.`mps'#c.lev_d2c_1lag_std

********************************************************************************
********************************************************************************
********************************************************************************
*Read-in ivol data (contains all trading days for Compustat firms)
use "${data}final_daily_data", clear
drop pre_crisis post_crisis crisis_flg

*destring gvkey, gen(gvkey_str)
*drop gvkey
*rename gvkey_str gvkey
gen 	pre_crisis = (date < date("01JUL2008","DMY"))

gen 	post_crisis = (date > date("30JUN2009","DMY")) & !missing(date)

gen 	crisis_flg = (date >= date("01JUL2008","DMY") & date <= date("30JUN2009","DMY"))

*Keep pre-crisis sample only
keep if pre_crisis & fomc

gen fyear = year

gen sic_fire 	 	 = 0 if sic_2dig!=.
replace sic_fire 	 = 1 if sic_2dig>=60 & sic_2dig<=67

sort cusip_8 date

merge m:1 cusip month year using "${data}distance2default", keepusing(cusip year month dd dd_lag)
drop if _merge==2
drop _merge


*Set sample
*Only 1-day lagged value of ivol
keep if gap_dist_`matur'==0
*Drop outliers
drop if divol_`matur'_outlier

***Standardize quarterly Compustat variables and lagged ivol level
foreach i in log_salesg_real_1lag size_assets_1lag liquidity_1lag pcmq_1lag recpay2yq_1lag d2aq_1lag ln_market_cap_q_1lag tobins_q_1lag lev_d2c_1lag ivol_`matur'_lag dd_lag age {

	*Pre-Crisis standardization
	sum `i'
	gen `i'_std = (`i' - r(mean)) / r(sd)
	
}

*Standardize ivol and dd
_pctile ivol_`matur'_lag, percentiles(30 70)
gen ivol_`matur'_hi2lo = (ivol_`matur'_lag - r(r1)) / (r(r2) -  r(r1))

_pctile dd_lag, percentiles(30 70)
gen dd_hi2lo = (dd_lag - r(r1)) / (r(r2) -  r(r1))

*Standardize mps to unit standard deviation & positive is expansionary
egen date_tag=tag(date)

sum `mps' if pre_crisis & date_tag
replace `mps' = -`mps'/r(sd)

keep if !unscheduled
qui: outreg2 using "${output}tableA1_panelC.xls", replace sum(log) eqkeep(N mean sd) keep(firm_stock_chg)


